﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;

namespace Repetition.Domain
{
    public class Problem
    {
        public enum DifficultyLevel { Easy, Normal, Hard }
        public enum ImportanceLevel { Important, Normal, Unimportant }

        private ICollection<Subject> subjects = new List<Subject>();

        public Problem() { }

        public Problem(string question, string answer)
        {
            Question = question;
            Answer = answer;
        }

        public virtual int Id { get; set; }
        public virtual string Question { get; set; }
        public virtual string Answer { get; set; }
        public virtual DifficultyLevel Difficulty { get; set; }
        public virtual ImportanceLevel Importance { get; set; }

        public virtual ReadOnlyCollection<Subject> Subjects
        {
            get
            {
                return subjects.ToList<Subject>().AsReadOnly();
            }
        }

        virtual public void AddSubject(Subject subject)
        {
            subjects.Add(subject);
        }

        virtual public bool RemoveSubject(Subject subject)
        {
            return subjects.Remove(subject);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
                return false;

            Problem right = (Problem)obj;

            if (subjects.Count == right.subjects.Count)
            {
                foreach (var subject in subjects)
                    if (!right.HasSubject(subject.Name)) return false;

                return Question == right.Question && Answer == right.Answer;
            }

            return false;
        }

        public override int GetHashCode()
        {
            return Question.GetHashCode();
        }

        public virtual bool HasSubject(string name)
        {
            foreach (var subject in subjects)
                if (subject.Name == name) return true;

            return false;
        }

        public virtual void Copy(Problem problem)
        {
            this.Answer = problem.Answer;
            this.Question = problem.Question;
            this.Difficulty = problem.Difficulty;
            this.Importance = problem.Importance;

            this.subjects.Clear();
            foreach (var subject in problem.Subjects)
                this.AddSubject(subject);
        }

        public virtual void RemoveAllSubjects()
        {
            subjects.Clear();
        }
    }
}